package com.prime.tutorial.moviestore.view.auth;

import com.prime.tutorial.moviestore.Constants;
import com.prime.tutorial.moviestore.domain.Users;
import com.prime.tutorial.moviestore.domain.UserActivity;
import com.prime.tutorial.moviestore.service.auth.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.Date;

/**
 * Managing jsf-Bean for login
 * Created by IntelliJ IDEA.
 * Date: 21 лют 2010
 * Time: 0:29:50
 * @author iryna.masyuk
 */
@Component("login")
@Scope("request")
public class Login  implements Serializable {
    private String loginname;
    private String password;
    AuthService authService;

    @Autowired
    public Login(AuthService authService) {
        this.authService = authService;
    }

    public String loginUser(){
        //if login or password are not filled
        if (loginname.equals("") || password.equals("")){
            showMessage("Please fill in all the fields");
            return null;
        }
        Users u = authService.login(loginname, password);
        //if users does not exist or password is incorrect
        if (u==null) {
            showMessage("Users does not exist or the password is incorrect");
            return null;
        };
        //if the users is not enabled
        if(!u.getEnabled()){
            showMessage("Users is disabled");
            return null;
        }

        //users exists
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().put(Constants.USER_SESSION_KEY, u);

        //creating useractivity
        //TODO test useractivity
        UserActivity userActivity = new UserActivity();
        userActivity.setUser(u);
        userActivity.setLogin(new Date());

        //TODO logout can't be null
        //userActivity.setLogout(new Date(0));

        //putting useractivity in session
        context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().put(Constants.USERACTIVITY_SESSION_KEY, userActivity);

        //persisting useractivity
        authService.createUserActivity(userActivity);

        return navigate(u);

    }

    /**
     * Navigates the users according to the group
     * @param u - Users
     * @return resulting page
     */
    private String navigate(Users u){
        //TODO very very lame. any prettier solution for navigation?
         if (u.getGroup().getName().equals(Constants.GROUP_ADMIN_NAME)) {
            return "adminDefault";
        }else if (u.getGroup().getName().equals(Constants.GROUP_NETWPLANNING_NAME)){
            return "netwPlanningDefault";
        }
        else if (u.getGroup().getName().equals(Constants.GROUP_LOGISTICS_NAME)){
            return "logisticsDefault";
        }
        else if (u.getGroup().getName().equals(Constants.GROUP_TECHSUPPORT_NAME)){
            return "techSupportDefault";
        }
        else if (u.getGroup().getName().equals(Constants.GROUP_REPORTS_NAME)){
            return "reportsDefault";
        }
        return null;
    }

    private void showMessage(String message){
       FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, "");
       FacesContext.getCurrentInstance().addMessage(null, facesMessage);
    }

    //---------------------------------getters&setters
    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
